package extreme.tddgildedrose;

/**
 * Created by 焦海源 on 2022/2/13.
 */
public class GildedRose {

    public static final String NORMAL_GOODS = "Normal goods";
    public static final String AGED_BRIE = "Aged Brie";
    public static final String SULFURAS = "Sulfuras";
    public static final String BACKSTAGE_PASS = "Backstage pass";

    public static int updateQuality(Goods goods) {
        int sellIn = goods.sellIn - 1;
        int oldQuality = goods.quality;
        int min = 0;
        int max = 50;
        String name = goods.name;
        int newQuality = 0;
        if (NORMAL_GOODS.equals(name)) {
            if (sellIn > 0) {
                newQuality = getNewQuality(oldQuality, -1);
            } else {
                newQuality = getNewQuality(oldQuality, -2);
            }
            if (newQuality < min) {
                newQuality = min;
            }
        }
        if (AGED_BRIE.equals(name)) {
            newQuality = getNewQuality(oldQuality, 1);
            if (newQuality > 50) {
                newQuality = max;
            }
        }
        if (SULFURAS.equals(name)) {
            newQuality = oldQuality;
        }
        if (BACKSTAGE_PASS.equals(name)) {
            if (sellIn >= 10) {
                newQuality = getNewQuality(oldQuality, 1);
            } else if (sellIn >= 5 && sellIn < 10){
                newQuality = getNewQuality(oldQuality, 2);
            } else if (sellIn >= 0 && sellIn < 5) {
                newQuality = getNewQuality(oldQuality, 3);
            } else {
                newQuality = min;
            }
            if (newQuality > max) {
                newQuality = max;
            }
        }
        return newQuality;
    }

    private static int getNewQuality(int oldQuality, int num) {
        return oldQuality + num;
    }

}
